#!/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器，163的话直接把QQ替换成163就好了，端口是25
mail_user = "********@qq.com"  # 用户名
mail_pass = "把上面生成的粘贴到这里"  # 口令，需要去qq邮箱设置里面生成动态授权码
#注意这里不是qq邮箱的密码，输入密码是连接不上滴

sender = '*********@qq.com'   #发件人
receivers = ['XXXXXXX@qq.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

message['From'] = Header("发件人名称", 'utf-8')
message['To'] = Header("收件人名称", 'utf-8')
subject = '主题'
email_body='邮件内容'
message = MIMEText(email_body, 'plain', 'utf-8')
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP_SSL(host=mail_host,port=465) #采用加密协议的smtp服务器
    smtpObj.login(mail_user,mail_pass)
    print('hahah')
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
